package com.egg.basic.enums.captcha;


import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.ObjectUtil;
import com.egg.common.swagger.annotation.SwaggerDisplayAnnotation;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

@Getter
@SwaggerDisplayAnnotation
@AllArgsConstructor
public enum CaptchaTypeEnum {


    MATH("数学", "math", MathGenerator.class),
    CHAR("随机字符串", "char", RandomGenerator.class),
    ;


    private String message;
    private String code;
    private final Class<? extends CodeGenerator> clazz;

    public static Optional<CaptchaTypeEnum> findByCode(String code) {
        if (ObjectUtil.isEmpty(code)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(o -> Objects.equals(code, o.getCode())).findAny();
    }

    @Override
    public String toString() {
        return message + ":" + code;
    }
}